package threadtest.waitexam;

public class Q {
	int n;
	boolean valueSet = false;

	synchronized int get() {
		if (valueSet == false)
			try {
				wait();
			} catch (InterruptedException e) {
				System.out.println("InterruptedException caught");
			}
		System.out.println("Got: " + this.n);

		valueSet = false;
		notify();
		return this.n;
	}

	synchronized void put(int n) {
		if (valueSet)
			try {
				wait();
			} catch (InterruptedException e) {
				System.out.println("InterruptedException caught");
			}
		this.n = n;
		valueSet = true;

		System.out.println("Put: " + n);
		notify();
	}
}
